package com.jingyanzi.resallocation.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.resallocation.dao.ResAssistantMapper;
import com.jingyanzi.resallocation.dao.ResSalesgroupMapper;
import com.jingyanzi.resallocation.domain.SalesGroupDO;
import com.jingyanzi.resallocation.entity.ResAssistant;
import com.jingyanzi.resallocation.entity.ResAssistantExample;
import com.jingyanzi.resallocation.entity.ResSalesgroup;
import com.jingyanzi.resallocation.entity.ResSalesgroupExample;
import com.jingyanzi.resallocation.entity.ResSalesgroupExample.Criteria;
import com.jingyanzi.resallocation.query.SalesGroupQueryDO;
import com.jingyanzi.resallocation.result.ResultDO;
import com.jingyanzi.resallocation.result.ResultSupport;
import com.jingyanzi.resallocation.service.ISalesGroupService;
import com.jingyanzi.resallocation.util.BeanUtilsExtends;

@Service("salesGroupService")
public class SalesGroupServiceImpl extends BaseServiceImpl implements ISalesGroupService {

	@Autowired
	private ResSalesgroupMapper salesgroupMapper;
	@Autowired
	private ResAssistantMapper assistantMapper;

	public ResultDO add(SalesGroupDO salesGroupDO) {
		ResSalesgroup salesgroup = new ResSalesgroup();
		SalesGroupDO salesGroupDO1 = new SalesGroupDO();
		ResultSupport result = BeanUtilsExtends.copy(salesgroup, salesGroupDO);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		salesgroup.setCreatetime(Calendar.getInstance().getTime());
		salesgroup.setIsDelete(1);
		if (salesgroup.getGroupId() == 0) {
			salesgroup.setCode(code() + "");
		} else {
			ResSalesgroup resSalesgroup = salesgroupMapper.selectByPrimaryKey(salesgroup.getGroupId());
			salesgroup.setCode(resSalesgroup.getCode() + "-" + code());
		}
		int r = -1;

		try {
			r = salesgroupMapper.insert(salesgroup);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加销售分组失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加销售分组失败");
			result.setSuccess(false);
			return result;
		}
		salesGroupDO = getSalesGroupDO(salesgroup);
		salesGroupDO1.setId(salesGroupDO.getId());
		result.setModel(ResultDO.FIRST_MODEL_KEY, salesGroupDO1);
		return result;
	}

	public ResultDO edit(SalesGroupDO salesGroupDO) {
		ResSalesgroup salesgroup = new ResSalesgroup();

		ResultSupport result = BeanUtilsExtends.copy(salesgroup, salesGroupDO);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		int r = -1;

		try {
			r = salesgroupMapper.updateByPrimaryKeySelective(salesgroup);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改销售分组失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改销售分组失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO del(Integer[] id) {
		ResSalesgroup salesgroup = new ResSalesgroup();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		for (int i = 0; i < id.length; i++) {
			salesgroup.setId(id[i]);
			salesgroup.setIsDelete(-1);
			int r = -1;
			try {
				r = salesgroupMapper.updateByPrimaryKeySelective(salesgroup);
			} catch (Exception e) {
				e.printStackTrace();
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除销售分组失败");
				result.setSuccess(false);
				return result;
			}

			if (r < 1) {
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除销售分组失败");
				result.setSuccess(false);
				return result;
			}
		}

		return result;
	}

	public ResultDO get(Integer id) {
		ResSalesgroup salesgroup = new ResSalesgroup();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		salesgroup.setId(id);
		salesgroup.setIsDelete(-1);

		try {
			salesgroup = salesgroupMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取销售分组失败");
			result.setSuccess(false);
			return result;
		}

		if (salesgroup == null) {
			result.setErrorCode(ResultDO.SALESGROUP_GET_ERROR);
			result.setErrorMsg("不存在该销售分组");
			result.setSuccess(false);
			return result;
		}
		SalesGroupDO salesGroupDO = getSalesGroupDO(salesgroup);
		result.setModel(ResultDO.FIRST_MODEL_KEY, salesGroupDO);
		return result;
	}

	public ResultDO query(SalesGroupQueryDO salesGroupQueryDO) {
		ResultSupport result = new ResultSupport();

		List<ResSalesgroup> list = null;

		ResSalesgroupExample example = new ResSalesgroupExample();

		Criteria createCriteria = example.createCriteria();
		if (salesGroupQueryDO.getMappingId() != null && salesGroupQueryDO.getMappingId() != 0) {
			createCriteria.andMappingIdEqualTo(salesGroupQueryDO.getMappingId());
		}
		if (salesGroupQueryDO.getGroupId() != null && salesGroupQueryDO.getGroupId() != 0) {
			createCriteria.andGroupIdEqualTo(salesGroupQueryDO.getGroupId());
		}
		createCriteria.andIsDeleteEqualTo(1);
		List<SalesGroupDO> salesGroupDOs = null;
		if (salesGroupQueryDO.isPage()) {
			example.setLimitByPage(salesGroupQueryDO.getStart() + "," + salesGroupQueryDO.getPageRows());
			long count = salesgroupMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = salesgroupMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取销售分组失败");
			result.setSuccess(false);
			return result;
		}

		salesGroupDOs = getSalesGroupDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, salesGroupDOs);
		return result;
	}

	@Override
	public ResultDO list(Integer id) {
		ResultSupport result = new ResultSupport();

		result.setModel(ResultSupport.FIRST_MODEL_KEY, listSalesGroup(id));

		return result;
	}

	public List<SalesGroupDO> listSalesGroup(Integer id) {

		List<ResSalesgroup> list = null;
		ResSalesgroupExample example = new ResSalesgroupExample();

		example.createCriteria().andGroupIdEqualTo(id).andIsDeleteEqualTo(1);

		try {
			list = salesgroupMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}

		List<SalesGroupDO> salesGroupDOs = new ArrayList<SalesGroupDO>();

		if (list == null || list.size() == 0) {
			return salesGroupDOs;
		}

		for (ResSalesgroup salesgroup : list) {
			SalesGroupDO salesGroupDO = getSalesGroupDO(salesgroup);
			salesGroupDO.setChildren(listSalesGroup(salesgroup.getId()));
			salesGroupDOs.add(salesGroupDO);
		}

		return salesGroupDOs;
	}

	@Override
	public ResultDO isDel(Integer id) {
		ResultSupport result = new ResultSupport();
		SalesGroupDO saleGroupDO = new SalesGroupDO();
		List<ResAssistant> list = null;
		List<ResSalesgroup> salesgroups = null;
		ResAssistantExample resAssistantExample = new ResAssistantExample();

		ResSalesgroupExample example = new ResSalesgroupExample();

		resAssistantExample.createCriteria().andIsDeleteEqualTo(1).andGroupIdEqualTo(id);
		example.createCriteria().andGroupIdEqualTo(id).andIsDeleteEqualTo(1);
		try {
			 list = assistantMapper.selectByExample(resAssistantExample);
			 salesgroups = salesgroupMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "验证出错");
			result.setSuccess(false);
			return result;
		}

		int isHave = 0;
		if (list.size() != 0) {
			isHave = 1;
		} else if (salesgroups.size() != 0) {
			isHave = 1;
		}
		saleGroupDO.setIsHave(isHave);
		result.setModel(ResultDO.FIRST_MODEL_KEY, saleGroupDO);
		return result;

	}

	public Integer code() {
		return (int) (Math.random() * (999 - 100)) + 100;
	}

	@Override
	public ResultDO getGrouping(String code) {
		ResultSupport result = new ResultSupport();

		List<ResSalesgroup> list = null;

		ResSalesgroupExample example = new ResSalesgroupExample();

		Criteria createCriteria = example.createCriteria();
		createCriteria.andCodeLike(code);
		createCriteria.andIsDeleteEqualTo(1);
		List<SalesGroupDO> salesGroupDOs = null;
		try {
			list = salesgroupMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取销售分组下所有分组失败");
			result.setSuccess(false);
			return result;
		}

		salesGroupDOs = getSalesGroupDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, salesGroupDOs);
		return result;
	}

}
